package cn.ydxiaoshuai.util;

import cn.hutool.http.HttpUtil;
import cn.ydxiaoshuai.po.JSAPIPageBean;
import cn.ydxiaoshuai.po.WXAccessToken;
import cn.ydxiaoshuai.po.WXJSAPITicket;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.UUID;

/**
 * @author 小帅丶
 * @Description 微信工具类
 * @className WXTemplatUtil
 * @Date 2019/11/28-9:51
 **/
@Component
public class WXUtil {


    /**
     * @Description 获取AccessToken
     * @Author 小帅丶
     * @Date  2019/11/28 9:58
     * @param appid 微信APPID
     * @param appsecret 微信APPSECRET
     * @return java.lang.String
     **/
    public String getAccessToken(String appid, String appsecret) {
        String apiUrl = WXAPI.GET_TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret);
        String result = HttpUtil.get(apiUrl);
        return result;
    }
    /**
     * @Description 获取AccessToken
     * @Author 小帅丶
     * @Date  2019/11/28 9:58
     * @param appid 微信APPID
     * @param appsecret 微信APPSECRET
     * @return java.lang.String
     **/
    public WXAccessToken getAccessTokenByBean(String appid, String appsecret) {
        String apiUrl = WXAPI.GET_TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret);
        String result = HttpUtil.get(apiUrl);
        WXAccessToken wxAccessToken = JSONObject.parseObject(result, WXAccessToken.class);
        return wxAccessToken;
    }
    /**
     * @Description 获取AccessToken
     * @Author 小帅丶
     * @Date  2019/11/28 9:58
     * @param appid 微信APPID
     * @param appsecret 微信APPSECRET
     * @return JSONObject
     **/
    public JSONObject getAccessTokenByJSON(String appid, String appsecret) {
        String apiUrl = WXAPI.GET_TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret);
        String result = HttpUtil.get(apiUrl);
        return JSONObject.parseObject(result);
    }
    /**
     * @Description 获取JSAPITicket
     * @Author 小帅丶
     * @Date  2019/11/28 9:58
     * @param accessToken 微信accessToken
     * @return WXJSAPITicket
     **/
    public WXJSAPITicket getJSTicket(String accessToken) {
        String apiUrl = WXAPI.GET_TICKET_URL.replace("ACCESS_TOKEN", accessToken);
        String result = HttpUtil.get(apiUrl);
        WXJSAPITicket wxjsapiTicket = JSONObject.parseObject(result, WXJSAPITicket.class);
        return wxjsapiTicket;
    }
    /**
     * @description 获取JSAPI
     * 此方法只是简单示例 正式使用建议封装一下
     * @Author 小帅丶
     * @Date  2019/12/2 14:00
     * @param appId
     * @param appSecret
     * @param url 当前请求的地址
     * @return cn.ydxiaoshuai.po.JSAPIPageBean
     **/
    public JSAPIPageBean getBean(String appId,String appSecret,String url){
        JSAPIPageBean bean = new JSAPIPageBean();
        WXAccessToken accessTokenByBean = getAccessTokenByBean(appId, appSecret);
        WXJSAPITicket jsTicket = getJSTicket(accessTokenByBean.getAccess_token());
        String jsapi_ticket = jsTicket.getTicket();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        //签名值
        String signature = "";
        // 注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str
                + "&timestamp=" + timestamp + "&url=" + url;
        System.out.println("string1 = " + string1);
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        bean.setUrl(url);
        bean.setAppId(appId);
        bean.setJsapi_ticket(jsapi_ticket);
        bean.setTimestamp(timestamp);
        bean.setNoncestr(nonce_str);
        bean.setSignature(signature);
        return bean;
    }
    /**
     * @Description byte转16进制字符
     * @Author 小帅丶
     * @Date  2019/12/2 14:41
     * @param hash 要转换的byte
     * @return java.lang.String
     **/
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
    /**
     * @Description 随机字符串
     * @Author 小帅丶
     * @Date  2019/12/2 14:42
     * @return java.lang.String
     **/
    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }
    /**
     * @Description 时间戳截止到秒
     * @Author 小帅丶
     * @Date  2019/12/2 14:43
     * @return java.lang.String
     **/
    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
}
